package com.custompro98.flashlight;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.widget.Button;

public class MainActivity extends Activity {

    Button btnSwitch;
    private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    Parameters params;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        btnSwitch = (Button) findViewById(R.id.btnSwitch);
        btnSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isFlashOn) {
                    turnFlashOff();
                }
                else {
                    turnFlashOn();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            }
            catch (RuntimeException e) {
                Log.e("Camera error. Failed to open. Error: ", e.getMessage());
            }
        }
    }

    private void turnFlashOn() {
        if (!isFlashOn) {
            if (camera == null || params == null) {
                return;
            }

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;

            toggleButtonText();
        }
    }

    private void turnFlashOff() {
        if (isFlashOn) {
            if (camera == null || params == null) {
                return;
            }
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;

            toggleButtonText();
        }
    }

    private void toggleButtonText() {
        if (isFlashOn) {
            btnSwitch.setText(R.string.turn_off);
        }
        else {
            btnSwitch.setText(R.string.turn_on);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        turnFlashOff();

        camera.release();
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        if (hasFlash) {
            turnFlashOn();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCamera();

        if (hasFlash) {
            turnFlashOn();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        getCamera();

        try {
            turnFlashOn();
        }
        catch (RuntimeException e){
            Log.e("Flash start failed.", e.getMessage());
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
}
